Unconstrained use of goto
can lead to programs that are extremely difficult to comprehend and analyse. For C++, it can also lead to
the program exhibiting unspecified behavior.
However, in many cases a total ban on goto
requires the introduction of flags to ensure correct control flow, and it is possible that
these flags may themselves be less transparent than the goto
they replace.
Therefore, the restricted use of goto
is allowed where that use will not lead to semantics contrary to developer expectations. "Back"
jumps are prohibited, since they can be used to create iterations without using the well-defined iteration statements supplied by the core
language.
Noncompliant code example
int f() {
int j = 0;
L1:
++j;
if (10 == j) {
goto L2; // forward jump ignored
}
// ...
goto L1; // Noncompliant
L2:
return ++j;
}
Compliant solution
int f() {
for (int j = 0; j < 11; j++) {
// ...
}
return ++j;
}